package com.ztesoft.sca.controller;

import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.model.ImButtonDto;
import com.ztesoft.sca.model.ImMenuDto;
import com.ztesoft.sca.model.ImPageDto;
import com.ztesoft.sca.service.ImMenuManageService;
import com.ztesoft.sca.util.message.ResponseUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理接口
 *
 * @author guojd
 */
@RestController
public class ImMenuManageController {
    @Autowired
    ImMenuManageService imMenuManageService;

    /**
     * 新增菜单
     *
     * @param imMenuDto 菜单对象
     */
    @RequestMapping(value = "/imMenuManage/insertMenu", method = {RequestMethod.POST})
    public String insertMenu(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);

        ImMenuDto imMenuDto = new ImMenuDto();
        if (!json.containsKey("menuCode") || !json.containsKey("menuName")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        imMenuDto.setMenuCode(json.getString("menuCode"));
        imMenuDto.setMenuName(json.getString("menuName"));
        try {
            imMenuDto.setParentId(json.getLong("parentId"));
        } catch (Exception e) {
            imMenuDto.setParentId(null);
        }
        //获取菜单ID
        Long menuId = imMenuManageService.getNewMenuId();
        if (menuId == -1) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        imMenuDto.setMenuId(menuId);
        imMenuManageService.insertMenu(imMenuDto);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     */
    @RequestMapping(value = "/imMenuManage/deleteMenu", method = {RequestMethod.POST})
    public String deleteMenu(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        if (!json.containsKey("menuId")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        } else {
            imMenuManageService.deleteMenu(json.getLong("menuId"));
            return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
        }
    }

    /**
     * 修改菜单
     *
     * @param imMenuDto 菜单对象
     */
    @RequestMapping(value = "/imMenuManage/updateMenu", method = {RequestMethod.POST})
    public String updateMenu(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        ImMenuDto imMenuDto = new ImMenuDto();
        imMenuDto.setMenuCode(json.getString("menuCode"));
        imMenuDto.setMenuName(json.getString("menuName"));
        try {
            imMenuDto.setParentId(json.getLong("parentId"));
        } catch (Exception e) {
            imMenuDto.setParentId(null);
        }
        imMenuDto.setMenuId(json.getLong("menuId"));
        imMenuManageService.updateMenu(imMenuDto);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 查询菜单
     *
     * @param param 查询参数
     *              menuName/menuId/parentId/menuCode
     * @return 菜单列表
     */
    @RequestMapping(value = "/imMenuManage/selectMenus", method = {RequestMethod.POST})
    public String selectMenus(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        Map<String, Object> param = new HashMap<>();
        if (json.containsKey("menuCode")) {
            param.put("menuCode", json.getString("menuCode"));
        }
        if (json.containsKey("menuName")) {
            param.put("menuName", json.getString("menuName"));
        }
        if (json.containsKey("parentId")) {
            if (json.getString("parentId").length() == 0) {
                param.put("parentId", null);
            } else {
                param.put("parentId", json.getLong("parentId"));
            }
        }
        if (json.containsKey("menuId") && json.getString("menuId").length() > 0) {
            param.put("menuId", json.getLong("menuId"));
        }
        List<ImMenuDto> imMenuDtoList = imMenuManageService.selectMenus(param);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imMenuDtoList);
    }

    /**
     * 查询菜单
     *
     * @param menuId 菜单ID
     * @return 菜单列表
     */
    @RequestMapping(value = "/imMenuManage/selectMenusDetail", method = {RequestMethod.POST})
    public String selectMenusDetail(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        Map<String, Object> param = new HashMap<>();
        param.put("menuId", json.getLong("menuId"));
        List<ImMenuDto> imMenuDtoList = imMenuManageService.selectMenus(param);
        if (imMenuDtoList.size() > 0) {
            return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imMenuDtoList.get(0));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 菜单列表树形
     *
     * @param parentId 父级ID，如果为空则查询全部
     * @return 菜单列表
     */
    @RequestMapping(value = "/imMenuManage/selectMenuTree", method = {RequestMethod.POST})
    public String selectMenuTree(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);

        if (!json.containsKey("parentId")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        Long parentId = null;
        if (json.getString("parentId").length() > 0) {
            parentId = json.getLong("parentId");
        }
        List<ImMenuDto> imMenuDtoList = imMenuManageService.selectMenuTree(parentId);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imMenuDtoList);
    }

    /**
     * 根据菜单ID查询按钮
     *
     * @param menuId 菜单ID
     * @return 按钮列表
     */
    @RequestMapping(value = "/imMenuManage/selectButtonsByMenuId", method = {RequestMethod.POST})
    public String selectButtonsByMenuId(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        Long menuId = null;
        if (json.containsKey("menuId")) {
            menuId = json.getLong("menuId");
        }
        List<ImButtonDto> imButtonDtoList = imMenuManageService.selectButtonsByMenuId(menuId);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imButtonDtoList);
    }

    /**
     * 菜单按钮关系新增
     *
     * @param menuId   菜单ID
     * @param buttonId 按钮ID
     */
    @RequestMapping(value = "/imMenuManage/insertButtonMenuRela", method = {RequestMethod.POST})
    public String insertButtonMenuRela(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        if (!json.containsKey("menuId") || !json.containsKey("buttonId")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        imMenuManageService.insertButtonMenuRela(json.getLong("menuId"), json.getLong("buttonId"));
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 删除菜单按钮关系
     *
     * @param menuId   菜单ID
     * @param buttonId 按钮ID
     */
    @RequestMapping(value = "/imMenuManage/deleteButtonMenuRela", method = {RequestMethod.POST})
    public String deleteButtonMenuRela(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        if (!json.containsKey("menuId") || !json.containsKey("buttonId")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        imMenuManageService.deleteButtonMenuRela(json.getLong("menuId"), json.getLong("buttonId"));
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 新增页面
     *
     * @param imPageDto 页面对象
     */
    @RequestMapping(value = "/imMenuManage/insertPage", method = {RequestMethod.POST})
    public String insertPage(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        if (!json.containsKey("menuId")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        ImPageDto imPageDto = new ImPageDto();
        imPageDto.setMenuId(json.getLong("menuId"));
        imPageDto.setPageCode(json.getString("pageCode"));
        imPageDto.setPageName(json.getString("pageName"));
        imPageDto.setTitle(json.getString("title"));
        imPageDto.setUrl(json.getString("url"));
        //获取菜单ID
        Long pageId = imMenuManageService.getNewPageId();
        if (pageId == -1) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        imPageDto.setPageId(pageId);
        imMenuManageService.insertPage(imPageDto);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 删除页面
     *
     * @param pageId 页面ID
     */
    @RequestMapping(value = "/imMenuManage/deletePage", method = {RequestMethod.POST})
    public String deletePage(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        if (!json.containsKey("pageId")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        imMenuManageService.deletePage(json.getLong("pageId"));
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 更新页面
     *
     * @param imPageDto 页面对象
     */
    @RequestMapping(value = "/imMenuManage/updatePage", method = {RequestMethod.POST})
    public String updatePage(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        if (!json.containsKey("menuId") || !json.containsKey("pageId")) {
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, null);
        }
        ImPageDto imPageDto = new ImPageDto();
        imPageDto.setMenuId(json.getLong("menuId"));
        imPageDto.setPageCode(json.getString("pageCode"));
        imPageDto.setPageName(json.getString("pageName"));
        imPageDto.setTitle(json.getString("title"));
        imPageDto.setUrl(json.getString("url"));
        imPageDto.setPageId(json.getLong("pageId"));
        imMenuManageService.updatePage(imPageDto);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, null);
    }

    /**
     * 查询页面列表
     *
     * @param param 参数
     *              pageId/menuId/pageCode/pageName/title/url
     * @return 页面列表
     */
    @RequestMapping(value = "/imMenuManage/selectPages", method = {RequestMethod.POST})
    public String selectPages(@RequestBody String requestBody, HttpServletRequest request) {
        JSONObject json = JSONObject.fromObject(requestBody);
        Map<String, Object> param = new HashMap<>();
        if (json.containsKey("url")) {
            param.put("url", json.getString("url"));
        }
        if (json.containsKey("title")) {
            param.put("title", json.getString("title"));
        }
        if (json.containsKey("pageName")) {
            param.put("pageName", json.getString("pageName"));
        }
        if (json.containsKey("pageCode")) {
            param.put("pageCode", json.getString("pageCode"));
        }
        if (json.containsKey("menuId")) {
            param.put("menuId", json.getLong("menuId"));
        }
        if (json.containsKey("pageId")) {
            param.put("pageId", json.getLong("pageId"));
        }
        List<ImPageDto> imPageDtoList = imMenuManageService.selectPages(param);
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, imPageDtoList);
    }
}
